package com.ruoyi.config;

import java.io.IOException;
import java.util.List;

import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {

	@Override
	public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
		if (resource == null) {
			return super.createPropertySource(name, resource);
		} else {
			YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
			List<PropertySource<?>> values = sourceLoader.load(name, resource.getResource());
			if (!values.isEmpty()) {
				return values.get(0);
			} else {
				return super.createPropertySource(name, null);

			}
		}

	}

}
